package com.gee.spring.cloud.feign.flow.work.impl;

import com.gee.spring.cloud.feign.flow.execute.ExecuteContext;
import com.gee.spring.cloud.feign.flow.handler.DefaultWorkHandler;
import com.gee.spring.cloud.feign.flow.result.ExecuteState;
import com.gee.spring.cloud.feign.flow.result.WorkResult;
import com.gee.spring.cloud.feign.flow.work.AbstractWork;

import java.lang.reflect.Method;

/**
 * desc:
 *
 * @author gee
 * @since 2021-04-01 16:20:12
 */
public class InitWork extends AbstractWork<Object> {

    public static final String INIT_WORK_ID = "initWork";

    @Override
    public Object process(Object target, Method method, Object[] args) throws Exception {
        return null;
    }

    @Override
    public void accept(ExecuteContext executeContext, Throwable throwable) {
        initExecuteContext(executeContext);
        WorkResult workResult = executeContext.getWorkResultMap().get(getId());
        workResult.getExecuteState().compareAndSet(ExecuteState.INIT.getCode(), ExecuteState.WORKING.getCode());
        executeContext.getGlobalExecuteState().compareAndSet(ExecuteState.INIT.getCode(), ExecuteState.WORKING.getCode());
        this.getWorkHandler().handleAfterFinish(this, executeContext);
    }

    @Override
    public String getId() {
        return INIT_WORK_ID;
    }

    public static InitWork build(){
        InitWork initWork = new InitWork();
        initWork.setWorkHandler(new DefaultWorkHandler());
        return initWork;
    }
}
